unit spr_OtherDogovors;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, shbl_Directory, PropFilerEh, XLSExportComp, PropStorageEh,
  FIBDatabase, pFIBDatabase, ExtCtrls, DB, FIBDataSet, pFIBDataSet,
  ActnList, Buttons, GridsEh, DBGridEh, StdCtrls, ComCtrls, ToolWin,
  DBCtrlsEh, Mask, DBLookupEh, DBGridEhGrouping;

type
  TsprOtherDogovors = class(TshblDirectory)
    dsReferID: TFIBIntegerField;
    dsReferUSERORG_ID: TFIBIntegerField;
    dsReferRPT_GROUP_ID: TFIBIntegerField;
    dsReferDOGHEADER: TFIBStringField;
    dsReferPAY_RATE: TFIBBCDField;
    dsReferIS_ACTIVE: TFIBSmallIntField;
    dsReferORG_NAME: TFIBStringField;
    dsReferGROUP_NAME: TFIBStringField;
    ds_Groups: TpFIBDataSet;
    dsc_Groups: TDataSource;
    ds_Orgs: TpFIBDataSet;
    dsc_Orgs: TDataSource;
    lcbGroupName: TDBLookupComboboxEh;
    lcbOrgName: TDBLookupComboboxEh;
    dbeHeader: TDBEditEh;
    dbneRate: TDBNumberEditEh;
    cbActive: TDBCheckBoxEh;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    ds_GroupsID: TFIBIntegerField;
    ds_GroupsNAME: TFIBStringField;
    ds_GroupsIS_ACTIVE: TFIBSmallIntField;
    ds_OrgsID: TFIBIntegerField;
    ds_OrgsNAME: TFIBStringField;
    GroupBox4: TGroupBox;
    dsAgents: TpFIBDataSet;
    dscAgents: TDataSource;
    dbgAgents: TDBGridEh;
    dsAgentsID: TFIBIntegerField;
    dsAgentsNAME: TFIBStringField;
    dsAgentsRTP_DOG_ID: TFIBIntegerField;
    dsAgentsUSERORG_ID: TFIBIntegerField;
    dsAgentsACCESS: TFIBIntegerField;
    procedure FormShow(Sender: TObject);
    procedure dsAgentsFieldChange(Sender: TField);
    procedure dsAgentsBeforePost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  sprOtherDogovors: TsprOtherDogovors;

implementation

{$R *.dfm}

procedure TsprOtherDogovors.FormShow(Sender: TObject);
begin
  inherited;
  ds_Groups.Open;
  ds_Orgs.Open;
end;

procedure TsprOtherDogovors.dsAgentsFieldChange(Sender: TField);
begin

  if (Sender = dsAgentsACCESS) and not dsAgentsACCESS.IsNull then begin

    if dsAgentsACCESS.OldValue <> dsAgentsACCESS.NewValue then begin
      if dsAgentsACCESS.Value = 0 then
        dsAgents.Delete
      else if dsAgents.State in [dsInsert, dsEdit] then begin
        dsAgentsRTP_DOG_ID.Value := dsReferID.Value;
        dsAgentsUSERORG_ID.Value := dsAgentsID.Value;
        dsAgents.Post;
      end;
    end;
  end;
end;

procedure TsprOtherDogovors.dsAgentsBeforePost(DataSet: TDataSet);
begin
  if dsAgentsRTP_DOG_ID.AsInteger = 0 then begin
    if dsRefer.State = dsInsert then begin
      dsRefer.Post;
      dsRefer.Edit;
    end;

    dsAgentsRTP_DOG_ID.Value := dsReferID.Value;
  end;
end;

end.
